﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ListenMusic.Models.Reposistory.User;
using ListenMusic.Playlist_ServiceReference;
using System.ServiceModel;
using ListenMusic.User_Song_ServiceReference;
using System.Transactions;
using System.IO;
namespace ListenMusic.Controllers.User
{
    public class InfoController : ApplicationController
    {
        //
        // GET: /Info/
       private PlaylistReposistory _playlist;
        private UploadResposistory _upload;
        public InfoController()
        {
            _playlist = new PlaylistReposistory();
             _upload = new UploadResposistory();
        }

        public ActionResult Index()
        {
            return View();
        }
        [Authorize(Roles = "websiteuser")]
        public ActionResult Playlist()
        {
            try
            {

                ViewBag.playlist = _playlist.getPlaylist_ByMemberID(Session["user"].ToString());
                return View();
            }
            catch (FaultException ex)
            {
                return Redirect("../Shared/_Error.cshtml");
                throw ex;
            }
        }
        [Authorize(Roles = "websiteuser")]
        public ActionResult Listen(int plid)
        {
            try
            {
                Playlist playlist = _playlist.getPlayList_ByPlaylistID(plid);
                ViewBag.playlist = playlist;
                ViewBag.lstsong = _playlist.getSongInSpecificPlaylist(Session["user"].ToString(), plid);
                return View();
            }
            catch(FaultException ex)
            {
                return Redirect("../Shared/_Error.cshtml");
                throw ex;
            }
        }
        [Authorize(Roles = "websiteuser")]
        public JsonResult ProcessEditPlaylist(string pln, string op, int plid)
        {
            try
            {
                Playlist pl = new Playlist();
                pl.PlaylistID = plid;
                pl.PlaylistName = pln;
                pl._Status = Convert.ToBoolean(op);
                bool res = _playlist.EditPlaylist(pl);
                if (res)
                {
                    return Json("Thay Đổi Playlist Thành Công.");
                }
                else
                {
                    return Json("Thay Đổi Playlist Thất Bại,Vui Lòng Thử Lại Sau.");
                }
            }
            catch (FaultException ex)
            {
                return Json("Thay Đổi Playlist Thất Bại,Vui Lòng Thử Lại Sau.");
                throw ex;
            }
        }


        [Authorize(Roles = "websiteuser")]
        public ActionResult EditPlaylistPicture(int plid, HttpPostedFileBase file)
        {
            try
            {
                 if (file != null && file.ContentLength > 0)
                 {
                    using(TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
                    {
                        try
                        {
                            string songurl = Path.GetFileName(file.FileName).ToLower().Replace(" ", "-");
                            var fileName = _upload.RandomStr() + "-" + songurl;
                            bool res =_playlist.changePlaylist_Picture(plid,fileName);
                            var path = Path.Combine(Server.MapPath("~/Images/Playlist/"), fileName);
                            if(res)
                            {
                                file.SaveAs(path);
                                ts.Complete();
                            }
                            else
                            {
                                ts.Dispose();
                                
                            }
                        }
                        catch(FaultException ex)
                        {
                            ts.Dispose();
                            throw ex;
                        }
                    }
                   
                 }
            }
            catch(Exception ex)
            {
                
                throw new Exception(ex.Message);
            }
            return View();
        }
        [Authorize(Roles = "websiteuser")]
        public ActionResult EditPlaylist(int plid)
        {
            try
            {
               
                Playlist pl = _playlist.getPlayList_ByPlaylistID(plid);
                ViewBag.Playlist_Detail = _playlist.getSongInSpecificPlaylist(Session["user"].ToString(), plid);
                return View(pl);
            }
            catch (FaultException ex)
            {
               return Redirect("..Views/Shared/_Error.cshtml");
                throw ex;
            }
        }
        [Authorize(Roles = "websiteuser")]
        public ActionResult RemoveSong(int fid)
        {
            try
            {
                bool res = _playlist.removeSonginPlaylist(fid);
                if (res)
                {
                    return Json(true);
                }
                else
                {
                    return Json(false);
                }
            }
            catch (FaultException ex)
            {
                return Json(false);
                throw ex;
            }
        }
         
        [Authorize(Roles="websiteuser")]
        public ActionResult RemovePlaylist(int plid)
        {
            try
            {
                bool res = _playlist.removePlaylist(plid);
                if (res)
                {
                    return Json(true);
                }
                else
                {
                    return Json(false);
                }
            }
            catch (FaultException ex)
            {
                return Json(false);
                throw ex;
            }
        }
     
    }
}
